package lysjmaster.base.common.spring.pagination;

import cn.hutool.core.util.PageUtil;
import lombok.Getter;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Created by zk on 2019-12-05 2:45
 * @description
 */
@Getter
public class PageResponseVO<T> implements Serializable {
    private int totalElement;
    private int totalPage;
    private T data;

    public PageResponseVO(int totalElement, PageRequestDTO pageRequestDTO, T data) {
        this.totalElement = totalElement;
        this.data = data;
        this.totalPage = PageUtil.totalPage(totalElement, pageRequestDTO.getPageSize());
    }

    public List pagination(Collection<T> data, PageRequestDTO pageRequestDTO) {
        return pagination(data.stream(), pageRequestDTO);
    }

    public List pagination(Stream<T> data, PageRequestDTO pageRequestDTO) {
        return data
                .skip((pageRequestDTO.getPageNumber() - 1) * pageRequestDTO.getPageNumber())
                .limit(pageRequestDTO.getPageSize())
                .collect(Collectors.toList());
    }


}
